/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.controller;

import com.google.common.base.Splitter;
import com.koron.purpose.utils.BusinessCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;

import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.purpose.bean.ContractQueryBean;
import com.koron.purpose.bean.PurposeContractBean;
import com.koron.purpose.service.api.PurposeContractService;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import java.util.LinkedList;
import java.util.List;

@Api(tags = "purpose", description = "责任书")
@RestController
@RequestMapping("/purpose")
public class PurposeContractController {

    @Autowired
    private PurposeContractService service;

    @ApiOperation("获取详情")
    @ApiImplicitParam(name = "id", value = "档案id",  paramType = "query", dataType = "String")
    @RequestMapping(value = "/getDetails.htm", method = {RequestMethod.GET})
    public MessageBean<?> getDetails(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.queryById(id);
    }

    @ApiOperation("新增")
    @RequestMapping(value = "/insert.htm", method = {RequestMethod.POST})
    @Log(title = "责任书档案制定", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> insert(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestBody PurposeContractBean bean) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(com.koron.purpose.utils.BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(com.koron.purpose.utils.BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.insert(bean, user, "", "");
    }
	
	/*@ApiOperation("新增")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "title", value = "标题",  paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "note", value = "说明",  paramType = "query", dataType = "String")})
    @RequestMapping(value = "/insert.htm", method = { RequestMethod.POST })
    @Log(title = "新增责任档案", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> insert(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, PurposeContractBean bean,PurposeItemArray itemList,PurposeObjectArray objectList,String title,String note){
		List<PurposeItem> iList = new ArrayList<>();
		for(int i=0;i<itemList.getItemId().length;i++){
			PurposeItem item = new PurposeItem();
			if(itemList.getItemId() != null){
				item.setItemId(itemList.getItemId()[i]);
			}
			if(itemList.getItemName() != null){
				item.setItemName(itemList.getItemName()[i]);
			}
			if(itemList.getItemValue() != null){
				item.setItemValue(itemList.getItemValue()[i]);
			}
			if(itemList.getNote() != null){
				item.setNote(itemList.getNote()[i]);
			}
			if(itemList.getRule() != null){
				item.setRule(itemList.getRule()[i]);
			}
			iList.add(item);
		}
		bean.setItemList(iList);
		List<PurposeObject> oList = new ArrayList<>();
		for(int i=0;i<objectList.getStaffCode().length;i++){
			PurposeObject object = new  PurposeObject();
			if(objectList.getStaffCode() != null){
				object.setStaffCode(objectList.getStaffCode()[i]);
			}
			if(objectList.getStaffName() != null){
				object.setStaffName(objectList.getStaffName()[i]);
			}
			if(objectList.getAreaCode() != null){
				object.setAreaCode(objectList.getAreaCode()[i]);
			}
			if(objectList.getAreaName() != null){
				object.setAreaName(objectList.getAreaName()[i]);
			}
			if(objectList.getCorpCode() != null){
				object.setCorpCode(objectList.getCorpCode()[i]);
			}
			if(objectList.getCorpName() != null){
				object.setCorpName(objectList.getCorpName()[i]);
			}
			if(objectList.getDeptCode() != null){
				object.setDeptCode(objectList.getDeptCode()[i]);
			}
			if(objectList.getDeptName() != null){
				object.setDeptName(objectList.getDeptName()[i]);
			}
			if(objectList.getFile() != null){
				object.setFile(objectList.getFile()[i]);
			}
			if(objectList.getNote() != null){
				object.setNote(objectList.getNote()[i]);
			}
			oList.add(object);
		}
		bean.setObjectList(oList);
        return service.insert(bean, user,title,note);
    }*/

    @ApiOperation("删除")
    @ApiImplicitParam(name = "id", value = "档案id",  paramType = "query", dataType = "String")
    @RequestMapping(value = "/delete.htm", method = {RequestMethod.POST})
    @Log(title = "删除责任书", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<?> delete(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.delete(id);
    }

    @ApiOperation("提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "责任书id",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "title", value = "标题",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "note", value = "描述",  paramType = "query", dataType = "String")})
    @RequestMapping(value = "/submit.htm", method = {RequestMethod.POST})
    @Log(title = "责任书制定提交", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> submit(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id, String title, String note) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.submit(id, user, title, note);
    }


    @ApiOperation("责任书制定批量提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "purposeContractIds", value = "责任书档案ID字符串(多个以逗号分隔)", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "purposeContractType", value = "责任书制定类型(1-集团、2-片区、3-公司、4-部门、5-厂站、6-班组)", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "personCode", value = "流程第一个节点审批人code", required = true, paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "/batchSubmit.htm", method = {RequestMethod.POST})
    @Log(title = "责任书制定批量提交", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean batchSubmit(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                   @ApiIgnore String purposeContractIds, @ApiIgnore Integer purposeContractType,
                                   @ApiIgnore String personCode) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.batchSubmit(user, purposeContractIds, purposeContractType, personCode);
    }

    @ApiOperation("获取流程数据信息")
    @ApiImplicitParam(name = "ids", value = "档案ids", required = true, paramType = "query", dataType = "String")
    @RequestMapping(value = "/getFlowListMsg.htm", method = {RequestMethod.GET})
    public MessageBean<?> getFlowListMsg(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String ids) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), PurposeContractBean.class);
        if (null == user) {
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        List<PurposeContractBean> list = new LinkedList<>();
        List<String> idList = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(ids);
        MessageBean<PurposeContractBean> messageBean;
        for (String id : idList) {
            messageBean = service.queryById(id);
            if (messageBean.getCode() != 0) {
                msg.setCode(-1);
                msg.setDescription("获取流程数据信息失败");
                return msg;
            }
            list.add(messageBean.getData());
        }
        msg.setData(list);
        return msg;
    }

    @ApiOperation("责任书下发")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "责任书id",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "objectId", value = "甲方",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "objectName", value = "乙方",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "staffMsgs", value = "责任人，JSON数组字符串[{staffCode:'001',staffName:'xxx'}]",  paramType = "query", dataType = "String")})
    @RequestMapping(value = "/sign.htm", method = {RequestMethod.POST})
    @Log(title = "责任书下发", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> sign(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String id,
                               @ApiIgnore String objectId, @ApiIgnore String objectName, @ApiIgnore String staffMsgs) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.sign(user, id, objectId, objectName, staffMsgs);
    }

    @ApiOperation("修改")
    @RequestMapping(value = "/update.htm", method = {RequestMethod.POST})
    @Log(title = "修改档案", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> update(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestBody PurposeContractBean bean) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.update(bean, user);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "year", value = "年份",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "objectType", value = "签订类型：1:总部、2:片区、3:公司、4:部门、5:人员",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "fstatusflag", value = "流程状态",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "billType", value = "责任书类型：1制定，2档案",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  paramType = "query", dataType = "Integer")})
    @ApiOperation("责任制定台账")
    @RequestMapping(value = "/list.htm", method = {RequestMethod.GET})
    public MessageBean<?> list(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore ContractQueryBean queryBean) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        queryBean.setBillType(1);
        return service.list(queryBean);
    }

    @ApiImplicitParam(name = "id", value = "id",  paramType = "query", dataType = "String")
    @ApiOperation("签订")
    @RequestMapping(value = "/signed.htm", method = {RequestMethod.POST})
    @Log(title = "责任书签订", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> signed(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id, MultipartFile file) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.signed(id, file, user);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "year", value = "年份",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "objectType", value = "签订类型：1:总部、2:片区、3:公司、4:部门、5:人员",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "fstatusflag", value = "流程状态",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "billType", value = "责任书类型：1制定，2档案",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页",  paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数",  paramType = "query", dataType = "Integer")})
    @ApiOperation("责任档案台账")
    @RequestMapping(value = "/signList.htm", method = {RequestMethod.GET})
    public MessageBean<?> signList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                   @ApiIgnore ContractQueryBean queryBean) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        queryBean.setBillType(2);
        return service.signList(user, queryBean);
    }
}
